import json

class This:
        def __init__(self,js):
                self.thistype = js['ThisTpye']
                self.threshold = js['Threshold']
                self.taskid = js['TaskID']
class That:
        def __init__(self,js):
                self.taskid = js['TaskID']
                self.actiontype = js['ActionTpye']
                self.action = js['Action']

class Rule:
	def __init__(self,rulejs):
                rulejs = json.loads(rulejs)
                self.id = rulejs['_id']['$oid']
                self.name = rulejs['name']
                self.category = rulejs['category']
                self.status = rulejs['status']
                getthis = rulejs['thislist']
                getthat = rulejs['thatlist']
                thisFSM = []
                thatFSM = []
                thislist = []
                thatlist = []
                if getthis:
                        for t in getthis:
                                th = This(t)
                                thislist.append(th)
                                thisFSM.append(0)
                if getthat:
                        for t in getthat:
                                th = That(t)
                                thatlist.append(th)
                                thatFSM.append(0)
                self.thislist = thislist
                self.thatlist = thatlist
                self.thisFSM = thisFSM
                self.thatFSM = thatFSM

class RuleBox:
        def __init__(self,rl=[]):
                self.list = rl

        def add(self,ru):
                self.list.append(ru)

        def delete(self,rid):
                for sl in self.list:
                        if sl.id == rid:
                                self.list.remove(sl)
        def refresh(self,rl=[]):
                self.list = rl

        def count(self):
                return len(self.list)
                
	
